<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        body{
            margin: 0;/*去掉自带的8个像素外边距*/
            background-image: url("imgs/bg.jpg");
            background-size: cover; /*设置背景为封面*/
            text-align: center;
        }
        h1{
            font-size: 36px;
            color: #E6A23C;
            margin-bottom: 0;/*去掉下外边距*/
        }
        img{
            width: 400px;
            margin:5px;
        }
        h2{
            font-size: 16px;
            color: #E6A23C;
            margin-bottom: 0;
        }
        .el-input__inner{
            width: 340px;

        }
        #code{
            width: 220px;
        }

    </style>
</head>
<body>
<div id="app">
    <h1>老有所依 | 养老院管理系统</h1>
    <img src="imgs/logo.png"  alt="">
    <h2>MyCareHome</h2>
    <div v-if="isusernameLogin">
        <el-card style="width: 500px;height: 400px;
    margin: 0 auto;background-color: rgba(255,255,255,0.3);position: relative">
            <el-button-group>
                <el-button type="primary" style="position: absolute;top: 40px;left: 150px" @click="usernameLogin()">用户名密码登录</el-button>
                <el-button type="primary" style="position: absolute;top: 40px;left: 290px" @click="phoneNum()">手机验证登录</el-button>
            </el-button-group>
            <!--label-width="55px" 让lable和文本框显示在一行之内-->
            <el-form label-width="55px" style="width: 400px;margin: 50px auto">
                </template>
                <el-form-item label="用户名">
                    <el-input type="text" id="username" v-model="user.username" placeholder="请输入用户名"  @change="usernameEmpty()"></el-input>
                    <span v-if="isUsername" style="color: red;position: absolute;left: 230px">用户名格式错误！</span>
                </el-form-item>
                <el-form-item label="密码" style="display: flow">
                    <el-input type="password" id="password"  v-model="user.password" placeholder="请输入密码" @change="passwordEmpty()"></el-input>
                    <span v-if="isPassword" style="color: red;position: absolute;left: 240px">密码格式错误！</span>
                </el-form-item>
                <el-form-item label="角色">
                    <template>
                        <el-select v-model="user.role" placeholder="请选择">
                            <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item style="position: relative;right:100px;">
                    <el-checkbox v-model="user.rem" ><span style="font-size: 16px;color: #303133">记住用户名和身份</span></el-checkbox>
                </el-form-item>
                <a style="text-decoration: none;position:relative;left:170px;bottom:50px" href="/phoneNumLogin.html">忘了密码？</a>
                <el-form-item>
                    <el-button type="warning" @click="login()"
                               style="position:relative;right: 23px;bottom:30px">登录/注册</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
    <div v-else>
        <el-card style="width: 500px;height: 300px;
    margin: 0 auto;background-color: rgba(255,255,255,0.3);position: relative">
            <el-button-group>
                <el-button type="primary" style="position: absolute;top: 40px;left: 150px" @click="usernameLogin()">用户名密码登录</el-button>
                <el-button type="primary" style="position: absolute;top: 40px;left: 290px" @click="phoneNum()">手机验证登录</el-button>
            </el-button-group>
            <!--label-width="55px" 让lable和文本框显示在一行之内-->
            <el-form label-width="80px" style="width: 400px;margin: 50px auto">
                <el-form-item label="手机号">
                    <el-input  type="text" v-model="userPhoneNum.phoneNum" placeholder="请输入手机号"></el-input>
                </el-form-item>
                <el-form-item label="验证码">
                    <el-input type="text" id="code" style="position: relative;right: 50px" v-model="userPhoneNum.code"  placeholder="请输入获取的验证码"></el-input>
                    <el-button type="success" style="position: absolute;right: -20px;bottom: 0px" @click="code()">获取验证码</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type="warning" @click="phoneNumLogin()"
                               style="position:relative;right: 23px">登录</el-button>
                </el-form-item>

            </el-form>
        </el-card>
    </div>
</div>
</body>
<!--引入axios和vue框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: 'body>div',
        data: function () {
            return {
                user:{username:"",password:"",role:"",rem:false},
                userPhoneNum:{phoneNum:"",code:""},
                options: [{
                    value: '1',
                    label: '院长'
                }, {
                    value: '2',
                    label: '护工'
                }, {
                    value: '3',
                    label: '前台'
                }, {
                    value: '4',
                    label: '用户'
                }],
                isusernameLogin:true,
                isPassword:false,
                isUsername:false,
            }
        },
        methods:{
            usernameLogin(){
                v.isusernameLogin=true
            }
            ,
            phoneNum(){
                v.isusernameLogin=false;
            },
            usernameEmpty(){
                if (v.user.username.trim()!="") {
                    v.isUsername = false;
                    document.getElementById("username").classList.remove("ss");
                }else {
                    v.isUsername = true;
                    document.getElementById("username").classList.add("ss");
                }
            },
            passwordEmpty(){
                if (v.user.password.trim()!="") {
                    v.isPassword = false;
                    document.getElementById("password").classList.remove("ss");
                }else {
                    v.isPassword = true;
                    document.getElementById("password").classList.add("ss");
                }
            },
            login(){
                if (v.user.username.trim()==""){
                    let l= document.getElementById("username");
                    l.classList.add("ss");
                    v.isUsername=true;
                }
                if (v.user.password.trim()==""){
                    let l= document.getElementById("password");
                    l.classList.add("ss");
                    v.isPassword=true;
                    return;
                }
                //发出异步请求
                axios.post("/login",v.user).then(function (response) {
                    switch (response.data) {
                        case 1:
                            location.href="/admin.html";
                            break;
                        case 2:
                            location.href="/worker.html";
                            break;
                        case 3:
                            location.href="/reception.html"
                            break;
                        case 4:
                            location.href="/user.html";
                            break;
                        case 5:
                            v.$message("密码错误！！！");
                            break;
                        case 6:
                            location.href="/reg.html";
                            break;
                    }
                })
            },
            /*获取手机验证码方法*/
            code(){
                axios.get("/verificationCode?phoneNum="+v.userPhoneNum.phoneNum).then(function (){
                })
            },
            /*手机验证登录*/
            phoneNumLogin(){
                axios.post("/phoneNumLogin",v.userPhoneNum).then(function (response){
                    switch (response.data) {
                        case 1:
                            location.href="/admin.html";
                            break;
                        case 2:
                            location.href="/worker.html";
                            break;
                        case 3:
                            location.href="/reception.html"
                            break;
                        case 4:
                            location.href="/user.html";
                            break;
                        case 5:
                            v.$message("验证码错误！！！");
                            break;
                        case 6:
                            v.$message("该手机号未被注册！！！");
                            break;
                    }
                })
            }
        },
        created:function (){
            let arr=document.cookie.split(";")
            for(let cookie of arr){
                let cookieArr=cookie.split("=");
                let name=cookieArr[0].trim();
                let value=cookieArr[1];
                if (name=="username"){
                    this.user.username=value;
                }else if(name=="password") {
                    this.user.password=value;
                }else {
                    this.user.role=value;
                }
            }
        }

    })
</script>
</html>